package com.fjh.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjh.domain.UserAddr;
import com.fjh.mapper.UserAddrMapper;
import com.fjh.service.UserAddrService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
@CacheConfig(cacheNames = "com.fjh.service.impl.UserAddrServiceImpl")
public class UserAddrServiceImpl extends ServiceImpl<UserAddrMapper, UserAddr> implements UserAddrService {

    @Autowired
    private UserAddrMapper userAddrMapper;

    /**
     * 查询用户的所有收货地址
     * @return
     */
    @Override
    @Cacheable(key = "#userId")
    public List<UserAddr> getUserAllAddr(String userId) {
        return userAddrMapper.selectList(new LambdaQueryWrapper<UserAddr>()
                .eq(UserAddr::getUserId,userId)
                .eq(UserAddr::getStatus,1)
                .orderByDesc(UserAddr::getCommonAddr)
        );
    }

    @Override
    @CacheEvict(key = "#userId")
    public void changeUserDefaultAddr(String userId, Long id) {
        log.info("修改用户id为{}的默认收货地址为{}", userId, id);
        // 1.查询到旧的收货地址
        UserAddr oldAddr = userAddrMapper.selectOne(new LambdaQueryWrapper<UserAddr>()
                .eq(UserAddr::getUserId, userId)
                .eq(UserAddr::getCommonAddr, 1)
                .eq(UserAddr::getStatus, 1)
        );
        if (!ObjectUtils.isEmpty(oldAddr)) {
            if (oldAddr.getAddrId().equals(id)) {
                return;
            }
            UserAddr newAddr = userAddrMapper.selectById(id);
            if (!ObjectUtils.isEmpty(newAddr)) {
                // 改老的和新的
                // 旧的地址改成0  新的地址改成1
                oldAddr.setCommonAddr(0);
                oldAddr.setUpdateTime(new Date());
                oldAddr.setVersion(oldAddr.getVersion() + 1);
                int i = userAddrMapper.updateById(oldAddr);
                if (i > 0) {
                    // 查询新的
                    if (!ObjectUtils.isEmpty(newAddr)) {
                        newAddr.setCommonAddr(1);
                        newAddr.setUpdateTime(new Date());
                        newAddr.setVersion(newAddr.getVersion() + 1);
                        userAddrMapper.updateById(newAddr);
                        return;
                    }
                }

            }
        }
    }


    /**
     * 新增用户的收货地址
     * @param userAddr
     * @return
     */
    @Override
    @CacheEvict(key = "#userAddr.userId")
    public boolean save(UserAddr userAddr) {
        log.info("新增收货地址{}", JSON.toJSONString(userAddr));
        userAddr.setCreateTime(new Date());
        userAddr.setUpdateTime(new Date());
        userAddr.setVersion(0);
        userAddr.setStatus(1);
        //如果用户之前没有收货地址，则设置为默认地址
        //查询之前用户是否有收货地址
        Integer count = userAddrMapper.selectCount(new LambdaQueryWrapper<UserAddr>()
                .eq(UserAddr::getStatus, 1)
                .eq(UserAddr::getCommonAddr, 1)
        );
        if (count<=0){
            userAddr.setCommonAddr(1);
        }
        return userAddrMapper.insert(userAddr)>0;
    }
}


